home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / tclUnixAZ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  50.0 KB  |  2,007 lines

  1. /* 
  2.  * tclUnixAZ.c --
  3.  *
  4.  *    This file contains the top-level command procedures for
  5.  *    commands in the Tcl core that require UNIX facilities
  6.  *    such as files and process execution.  Much of the code
  7.  *    in this file is based on earlier versions contributed
  8.  *    by Karl Lehenbauer, Mark Diekhans and Peter da Silva.
  9.  *
  10.  * Copyright (c) 1991-1994 The Regents of the University of California.
  11.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  12.  *
  13.  * See the file "license.terms" for information on usage and redistribution
  14.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15.  */
  16.  
  17. #ifndef lint
  18. static char sccsid[] = "@(#) tclUnixAZ.c 1.83 95/02/20 16:35:24";
  19. #endif /* not lint */
  20.  
  21. #include "tclInt.h"
  22. #include "tclPort.h"
  23.  
  24. /*
  25.  * The variable is a secret trap-door used by the "fileevent" command
  26.  * in Tk to destroy file event bindings whenever a file is closed.  I
  27.  * realize that this is a big ugly...
  28.  */
  29.  
  30. void (*tcl_FileCloseProc) _ANSI_ARGS_((FILE *f)) = NULL;
  31.  
  32. /*
  33.  * The variable below caches the name of the current working directory
  34.  * in order to avoid repeated calls to getcwd.  The string is malloc-ed.
  35.  * NULL means the cache needs to be refreshed.
  36.  */
  37.  
  38. static char *currentDir =  NULL;
  39.  
  40. /*
  41.  * If the system doesn't define one or both of the errno values EAGAIN
  42.  * and EWOULDBLOCK, #define them to a bogus value that will never occur.
  43.  */
  44.  
  45. #ifndef EAGAIN
  46. #   define EAGAIN -1901
  47. #endif
  48. #ifndef EWOULDBLOCK
  49. #   define EWOULDBLOCK -1901
  50. #endif
  51.  
  52. /*
  53.  * Prototypes for local procedures defined in this file:
  54.  */
  55.  
  56. static int        CleanupChildren _ANSI_ARGS_((Tcl_Interp *interp,
  57.                 int numPids, int *pidPtr, int errorId,
  58.                 int keepNewline));
  59. static char *        GetFileType _ANSI_ARGS_((int mode));
  60. static char *        GetOpenMode _ANSI_ARGS_((Tcl_Interp *interp,
  61.                 char *string, int *modePtr));
  62. static int        StoreStatData _ANSI_ARGS_((Tcl_Interp *interp,
  63.                 char *varName, struct stat *statPtr));
  64.  
  65. /*
  66.  *----------------------------------------------------------------------
  67.  *
  68.  * Tcl_CdCmd --
  69.  *
  70.  *    This procedure is invoked to process the "cd" Tcl command.
  71.  *    See the user documentation for details on what it does.
  72.  *
  73.  * Results:
  74.  *    A standard Tcl result.
  75.  *
  76.  * Side effects:
  77.  *    See the user documentation.
  78.  *
  79.  *----------------------------------------------------------------------
  80.  */
  81.  
  82.     /* ARGSUSED */
  83. int
  84. Tcl_CdCmd(dummy, interp, argc, argv)
  85.     ClientData dummy;            /* Not used. */
  86.     Tcl_Interp *interp;            /* Current interpreter. */
  87.     int argc;                /* Number of arguments. */
  88.     char **argv;            /* Argument strings. */
  89. {
  90.     char *dirName;
  91.     Tcl_DString buffer;
  92.     int result;
  93.  
  94.     if (argc > 2) {
  95.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  96.         " dirName\"", (char *) NULL);
  97.     return TCL_ERROR;
  98.     }
  99.  
  100.     if (argc == 2) {
  101.     dirName = argv[1];
  102.     } else {
  103.     dirName = "~";
  104.     }
  105.     dirName = Tcl_TildeSubst(interp, dirName, &buffer);
  106.     if (dirName == NULL) {
  107.     return TCL_ERROR;
  108.     }
  109.     if (currentDir != NULL) {
  110.     ckfree(currentDir);
  111.     currentDir = NULL;
  112.     }
  113.     result = TCL_OK;
  114.     if (chdir(dirName) != 0) {
  115.     Tcl_AppendResult(interp, "couldn't change working directory to \"",
  116.         dirName, "\": ", Tcl_PosixError(interp), (char *) NULL);
  117.     result = TCL_ERROR;
  118.     }
  119.     Tcl_DStringFree(&buffer);
  120.     return result;
  121. }
  122.  
  123. /*
  124.  *----------------------------------------------------------------------
  125.  *
  126.  * Tcl_CloseCmd --
  127.  *
  128.  *    This procedure is invoked to process the "close" Tcl command.
  129.  *    See the user documentation for details on what it does.
  130.  *
  131.  * Results:
  132.  *    A standard Tcl result.
  133.  *
  134.  * Side effects:
  135.  *    See the user documentation.
  136.  *
  137.  *----------------------------------------------------------------------
  138.  */
  139.  
  140.     /* ARGSUSED */
  141. int
  142. Tcl_CloseCmd(dummy, interp, argc, argv)
  143.     ClientData dummy;            /* Not used. */
  144.     Tcl_Interp *interp;            /* Current interpreter. */
  145.     int argc;                /* Number of arguments. */
  146.     char **argv;            /* Argument strings. */
  147. {
  148.     OpenFile *oFilePtr;
  149.     int result = TCL_OK;
  150.     FILE *f;
  151.  
  152.     if (argc != 2) {
  153.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  154.         " fileId\"", (char *) NULL);
  155.     return TCL_ERROR;
  156.     }
  157.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  158.     return TCL_ERROR;
  159.     }
  160.     oFilePtr = tclOpenFiles[fileno(f)];
  161.     tclOpenFiles[fileno(f)] = NULL;
  162.  
  163.     /*
  164.      * First close the file (in the case of a process pipeline, there may
  165.      * be two files, one for the pipe at each end of the pipeline).  The
  166.      * calls to *tcl_FileCloseProc are a hack so that Tk's "fileevent"
  167.      * command can clean up its state, if any, when files are closed.
  168.      */
  169.  
  170.     if (oFilePtr->f2 != NULL) {
  171.     if (tcl_FileCloseProc != NULL) {
  172.         (*tcl_FileCloseProc)(oFilePtr->f2);
  173.     }
  174.     clearerr(oFilePtr->f2);
  175.     if (fclose(oFilePtr->f2) == EOF) {
  176.         Tcl_AppendResult(interp, "error closing \"", argv[1],
  177.             "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  178.         result = TCL_ERROR;
  179.     }
  180.     }
  181.     if (tcl_FileCloseProc != NULL) {
  182.     (*tcl_FileCloseProc)(oFilePtr->f);
  183.     }
  184.     clearerr(oFilePtr->f);
  185.     if (fclose(oFilePtr->f) == EOF) {
  186.     Tcl_AppendResult(interp, "error closing \"", argv[1],
  187.         "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  188.     result = TCL_ERROR;
  189.     }
  190.  
  191.     /*
  192.      * If the file was a connection to a pipeline, clean up everything
  193.      * associated with the child processes.
  194.      */
  195.  
  196.     if (oFilePtr->numPids > 0) {
  197.     if (CleanupChildren(interp, oFilePtr->numPids, oFilePtr->pidPtr,
  198.         oFilePtr->errorId, 0) != TCL_OK) {
  199.         result = TCL_ERROR;
  200.     }
  201.     }
  202.  
  203.     ckfree((char *) oFilePtr);
  204.     return result;
  205. }
  206.  
  207. /*
  208.  *----------------------------------------------------------------------
  209.  *
  210.  * Tcl_EofCmd --
  211.  *
  212.  *    This procedure is invoked to process the "eof" Tcl command.
  213.  *    See the user documentation for details on what it does.
  214.  *
  215.  * Results:
  216.  *    A standard Tcl result.
  217.  *
  218.  * Side effects:
  219.  *    See the user documentation.
  220.  *
  221.  *----------------------------------------------------------------------
  222.  */
  223.  
  224.     /* ARGSUSED */
  225. int
  226. Tcl_EofCmd(notUsed, interp, argc, argv)
  227.     ClientData notUsed;            /* Not used. */
  228.     Tcl_Interp *interp;            /* Current interpreter. */
  229.     int argc;                /* Number of arguments. */
  230.     char **argv;            /* Argument strings. */
  231. {
  232.     FILE *f;
  233.  
  234.     if (argc != 2) {
  235.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  236.         " fileId\"", (char *) NULL);
  237.     return TCL_ERROR;
  238.     }
  239.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  240.     return TCL_ERROR;
  241.     }
  242.     if (feof(f)) {
  243.     interp->result = "1";
  244.     } else {
  245.     interp->result = "0";
  246.     }
  247.     return TCL_OK;
  248. }
  249.  
  250. /*
  251.  *----------------------------------------------------------------------
  252.  *
  253.  * Tcl_ExecCmd --
  254.  *
  255.  *    This procedure is invoked to process the "exec" Tcl command.
  256.  *    See the user documentation for details on what it does.
  257.  *
  258.  * Results:
  259.  *    A standard Tcl result.
  260.  *
  261.  * Side effects:
  262.  *    See the user documentation.
  263.  *
  264.  *----------------------------------------------------------------------
  265.  */
  266.  
  267.     /* ARGSUSED */
  268. int
  269. Tcl_ExecCmd(dummy, interp, argc, argv)
  270.     ClientData dummy;            /* Not used. */
  271.     Tcl_Interp *interp;            /* Current interpreter. */
  272.     int argc;                /* Number of arguments. */
  273.     char **argv;            /* Argument strings. */
  274. {
  275.     int outputId;            /* File id for output pipe.  -1
  276.                      * means command overrode. */
  277.     int errorId;            /* File id for temporary file
  278.                      * containing error output. */
  279.     int *pidPtr;
  280.     int numPids, result, keepNewline;
  281.     int firstWord;
  282.  
  283.     /*
  284.      * Check for a leading "-keepnewline" argument.
  285.      */
  286.  
  287.     keepNewline = 0;
  288.     for (firstWord = 1; (firstWord < argc) && (argv[firstWord][0] == '-');
  289.         firstWord++) {
  290.     if (strcmp(argv[firstWord], "-keepnewline") == 0) {
  291.         keepNewline = 1;
  292.     } else if (strcmp(argv[firstWord], "--") == 0) {
  293.         firstWord++;
  294.         break;
  295.     } else {
  296.         Tcl_AppendResult(interp, "bad switch \"", argv[firstWord],
  297.             "\": must be -keepnewline or --", (char *) NULL);
  298.         return TCL_ERROR;
  299.     }
  300.     }
  301.  
  302.     if (argc <= firstWord) {
  303.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  304.         " ?switches? arg ?arg ...?\"", (char *) NULL);
  305.     return TCL_ERROR;
  306.     }
  307.  
  308.     /*
  309.      * See if the command is to be run in background;  if so, create
  310.      * the command, detach it, and return a list of pids.
  311.      */
  312.  
  313.     if ((argv[argc-1][0] == '&') && (argv[argc-1][1] == 0)) {
  314.     int i;
  315.     char id[50];
  316.  
  317.     argc--;
  318.     argv[argc] = NULL;
  319.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  320.         &pidPtr, (int *) NULL, (int *) NULL, (int *) NULL);
  321.     if (numPids < 0) {
  322.         return TCL_ERROR;
  323.     }
  324.     Tcl_DetachPids(numPids, pidPtr);
  325.     for (i = 0; i < numPids; i++) {
  326.         sprintf(id, "%d", pidPtr[i]);
  327.         Tcl_AppendElement(interp, id);
  328.     }
  329.     ckfree((char *) pidPtr);
  330.     return TCL_OK;
  331.     }
  332.  
  333.     /*
  334.      * Create the command's pipeline.
  335.      */
  336.  
  337.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  338.         &pidPtr, (int *) NULL, &outputId, &errorId);
  339.     if (numPids < 0) {
  340.     return TCL_ERROR;
  341.     }
  342.  
  343.     /*
  344.      * Read the child's output (if any) and put it into the result.
  345.      */
  346.  
  347.     result = TCL_OK;
  348.     if (outputId != -1) {
  349.     while (1) {
  350. #        define BUFFER_SIZE 1000
  351.         char buffer[BUFFER_SIZE+1];
  352.         int count;
  353.     
  354.         count = read(outputId, buffer, (size_t) BUFFER_SIZE);
  355.     
  356.         if (count == 0) {
  357.         break;
  358.         }
  359.         if (count < 0) {
  360.         Tcl_ResetResult(interp);
  361.         Tcl_AppendResult(interp,
  362.             "error reading from output pipe: ",
  363.             Tcl_PosixError(interp), (char *) NULL);
  364.         result = TCL_ERROR;
  365.         break;
  366.         }
  367.         buffer[count] = 0;
  368.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  369.     }
  370.     close(outputId);
  371.     }
  372.  
  373.     if (CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  374.         != TCL_OK) {
  375.     result = TCL_ERROR;
  376.     }
  377.     return result;
  378. }
  379.  
  380. /*
  381.  *----------------------------------------------------------------------
  382.  *
  383.  * Tcl_ExitCmd --
  384.  *
  385.  *    This procedure is invoked to process the "exit" Tcl command.
  386.  *    See the user documentation for details on what it does.
  387.  *
  388.  * Results:
  389.  *    A standard Tcl result.
  390.  *
  391.  * Side effects:
  392.  *    See the user documentation.
  393.  *
  394.  *----------------------------------------------------------------------
  395.  */
  396.  
  397.     /* ARGSUSED */
  398. int
  399. Tcl_ExitCmd(dummy, interp, argc, argv)
  400.     ClientData dummy;            /* Not used. */
  401.     Tcl_Interp *interp;            /* Current interpreter. */
  402.     int argc;                /* Number of arguments. */
  403.     char **argv;            /* Argument strings. */
  404. {
  405.     int value;
  406.  
  407.     if ((argc != 1) && (argc != 2)) {
  408.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  409.         " ?returnCode?\"", (char *) NULL);
  410.     return TCL_ERROR;
  411.     }
  412.     if (argc == 1) {
  413.     exit(0);
  414.     }
  415.     if (Tcl_GetInt(interp, argv[1], &value) != TCL_OK) {
  416.     return TCL_ERROR;
  417.     }
  418.     exit(value);
  419.     /*NOTREACHED*/
  420.     return TCL_OK;            /* Better not ever reach this! */
  421. }
  422.  
  423. /*
  424.  *----------------------------------------------------------------------
  425.  *
  426.  * Tcl_FileCmd --
  427.  *
  428.  *    This procedure is invoked to process the "file" Tcl command.
  429.  *    See the user documentation for details on what it does.
  430.  *
  431.  * Results:
  432.  *    A standard Tcl result.
  433.  *
  434.  * Side effects:
  435.  *    See the user documentation.
  436.  *
  437.  *----------------------------------------------------------------------
  438.  */
  439.  
  440.     /* ARGSUSED */
  441. int
  442. Tcl_FileCmd(dummy, interp, argc, argv)
  443.     ClientData dummy;            /* Not used. */
  444.     Tcl_Interp *interp;            /* Current interpreter. */
  445.     int argc;                /* Number of arguments. */
  446.     char **argv;            /* Argument strings. */
  447. {
  448.     char *p, *fileName;
  449.     int c, statOp, result;
  450.     size_t length;
  451.     int mode = 0;            /* Initialized only to prevent
  452.                      * compiler warning message. */
  453.     struct stat statBuf;
  454.     Tcl_DString buffer;
  455.  
  456.     if (argc < 3) {
  457.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  458.         " option name ?arg ...?\"", (char *) NULL);
  459.     return TCL_ERROR;
  460.     }
  461.     c = argv[1][0];
  462.     length = strlen(argv[1]);
  463.     result = TCL_OK;
  464.  
  465.     /*
  466.      * First handle operations on the file name.
  467.      */
  468.  
  469.     fileName = Tcl_TildeSubst(interp, argv[2], &buffer);
  470.     if (fileName == NULL) {
  471.     result = TCL_ERROR;
  472.     goto done;
  473.     }
  474.     if ((c == 'd') && (strncmp(argv[1], "dirname", length) == 0)) {
  475.     if (argc != 3) {
  476.         argv[1] = "dirname";
  477.         not3Args:
  478.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  479.             " ", argv[1], " name\"", (char *) NULL);
  480.         result = TCL_ERROR;
  481.         goto done;
  482.     }
  483.     p = strrchr(fileName, '/');
  484.     if (p == NULL) {
  485.         interp->result = ".";
  486.     } else if (p == fileName) {
  487.         interp->result = "/";
  488.     } else {
  489.         *p = 0;
  490.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  491.         *p = '/';
  492.     }
  493.     goto done;
  494.     } else if ((c == 'r') && (strncmp(argv[1], "rootname", length) == 0)
  495.         && (length >= 2)) {
  496.     char *lastSlash;
  497.  
  498.     if (argc != 3) {
  499.         argv[1] = "rootname";
  500.         goto not3Args;
  501.     }
  502.     p = strrchr(fileName, '.');
  503.     lastSlash = strrchr(fileName, '/');
  504.     if ((p == NULL) || ((lastSlash != NULL) && (lastSlash > p))) {
  505.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  506.     } else {
  507.         *p = 0;
  508.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  509.         *p = '.';
  510.     }
  511.     goto done;
  512.     } else if ((c == 'e') && (strncmp(argv[1], "extension", length) == 0)
  513.         && (length >= 3)) {
  514.     char *lastSlash;
  515.  
  516.     if (argc != 3) {
  517.         argv[1] = "extension";
  518.         goto not3Args;
  519.     }
  520.     p = strrchr(fileName, '.');
  521.     lastSlash = strrchr(fileName, '/');
  522.     if ((p != NULL) && ((lastSlash == NULL) || (lastSlash < p))) {
  523.         Tcl_SetResult(interp, p, TCL_VOLATILE);
  524.     }
  525.     goto done;
  526.     } else if ((c == 't') && (strncmp(argv[1], "tail", length) == 0)
  527.         && (length >= 2)) {
  528.     if (argc != 3) {
  529.         argv[1] = "tail";
  530.         goto not3Args;
  531.     }
  532.     p = strrchr(fileName, '/');
  533.     if (p != NULL) {
  534.         Tcl_SetResult(interp, p+1, TCL_VOLATILE);
  535.     } else {
  536.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  537.     }
  538.     goto done;
  539.     }
  540.  
  541.     /*
  542.      * Next, handle operations that can be satisfied with the "access"
  543.      * kernel call.
  544.      */
  545.  
  546.     if (fileName == NULL) {
  547.     result = TCL_ERROR;
  548.     goto done;
  549.     }
  550.     if ((c == 'r') && (strncmp(argv[1], "readable", length) == 0)
  551.         && (length >= 5)) {
  552.     if (argc != 3) {
  553.         argv[1] = "readable";
  554.         goto not3Args;
  555.     }
  556.     mode = R_OK;
  557.     checkAccess:
  558.     if (access(fileName, mode) == -1) {
  559.         interp->result = "0";
  560.     } else {
  561.         interp->result = "1";
  562.     }
  563.     goto done;
  564.     } else if ((c == 'w') && (strncmp(argv[1], "writable", length) == 0)) {
  565.     if (argc != 3) {
  566.         argv[1] = "writable";
  567.         goto not3Args;
  568.     }
  569.     mode = W_OK;
  570.     goto checkAccess;
  571.     } else if ((c == 'e') && (strncmp(argv[1], "executable", length) == 0)
  572.         && (length >= 3)) {
  573.     if (argc != 3) {
  574.         argv[1] = "executable";
  575.         goto not3Args;
  576.     }
  577.     mode = X_OK;
  578.     goto checkAccess;
  579.     } else if ((c == 'e') && (strncmp(argv[1], "exists", length) == 0)
  580.         && (length >= 3)) {
  581.     if (argc != 3) {
  582.         argv[1] = "exists";
  583.         goto not3Args;
  584.     }
  585.     mode = F_OK;
  586.     goto checkAccess;
  587.     }
  588.  
  589.     /*
  590.      * Lastly, check stuff that requires the file to be stat-ed.
  591.      */
  592.  
  593.     if ((c == 'a') && (strncmp(argv[1], "atime", length) == 0)) {
  594.     if (argc != 3) {
  595.         argv[1] = "atime";
  596.         goto not3Args;
  597.     }
  598.     if (stat(fileName, &statBuf) == -1) {
  599.         goto badStat;
  600.     }
  601.     sprintf(interp->result, "%ld", statBuf.st_atime);
  602.     goto done;
  603.     } else if ((c == 'i') && (strncmp(argv[1], "isdirectory", length) == 0)
  604.         && (length >= 3)) {
  605.     if (argc != 3) {
  606.         argv[1] = "isdirectory";
  607.         goto not3Args;
  608.     }
  609.     statOp = 2;
  610.     } else if ((c == 'i') && (strncmp(argv[1], "isfile", length) == 0)
  611.         && (length >= 3)) {
  612.     if (argc != 3) {
  613.         argv[1] = "isfile";
  614.         goto not3Args;
  615.     }
  616.     statOp = 1;
  617.     } else if ((c == 'l') && (strncmp(argv[1], "lstat", length) == 0)) {
  618.     if (argc != 4) {
  619.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  620.             " lstat name varName\"", (char *) NULL);
  621.         result = TCL_ERROR;
  622.         goto done;
  623.     }
  624.  
  625.     if (lstat(fileName, &statBuf) == -1) {
  626.         Tcl_AppendResult(interp, "couldn't lstat \"", argv[2],
  627.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  628.         result = TCL_ERROR;
  629.         goto done;
  630.     }
  631.     result = StoreStatData(interp, argv[3], &statBuf);
  632.     goto done;
  633.     } else if ((c == 'm') && (strncmp(argv[1], "mtime", length) == 0)) {
  634.     if (argc != 3) {
  635.         argv[1] = "mtime";
  636.         goto not3Args;
  637.     }
  638.     if (stat(fileName, &statBuf) == -1) {
  639.         goto badStat;
  640.     }
  641.     sprintf(interp->result, "%ld", statBuf.st_mtime);
  642.     goto done;
  643.     } else if ((c == 'o') && (strncmp(argv[1], "owned", length) == 0)) {
  644.     if (argc != 3) {
  645.         argv[1] = "owned";
  646.         goto not3Args;
  647.     }
  648.     statOp = 0;
  649.     } else if ((c == 'r') && (strncmp(argv[1], "readlink", length) == 0)
  650.         && (length >= 5)) {
  651.     char linkValue[MAXPATHLEN+1];
  652.     int linkLength;
  653.  
  654.     if (argc != 3) {
  655.         argv[1] = "readlink";
  656.         goto not3Args;
  657.     }
  658.  
  659.     /*
  660.      * If S_IFLNK isn't defined it means that the machine doesn't
  661.      * support symbolic links, so the file can't possibly be a
  662.      * symbolic link.  Generate an EINVAL error, which is what
  663.      * happens on machines that do support symbolic links when
  664.      * you invoke readlink on a file that isn't a symbolic link.
  665.      */
  666.  
  667. #ifndef S_IFLNK
  668.     linkLength = -1;
  669.     errno = EINVAL;
  670. #else
  671.     linkLength = readlink(fileName, linkValue, sizeof(linkValue) - 1);
  672. #endif /* S_IFLNK */
  673.     if (linkLength == -1) {
  674.         Tcl_AppendResult(interp, "couldn't readlink \"", argv[2],
  675.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  676.         result = TCL_ERROR;
  677.         goto done;
  678.     }
  679.     linkValue[linkLength] = 0;
  680.     Tcl_SetResult(interp, linkValue, TCL_VOLATILE);
  681.     goto done;
  682.     } else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)
  683.         && (length >= 2)) {
  684.     if (argc != 3) {
  685.         argv[1] = "size";
  686.         goto not3Args;
  687.     }
  688.     if (stat(fileName, &statBuf) == -1) {
  689.         goto badStat;
  690.     }
  691.     sprintf(interp->result, "%ld", statBuf.st_size);
  692.     goto done;
  693.     } else if ((c == 's') && (strncmp(argv[1], "stat", length) == 0)
  694.         && (length >= 2)) {
  695.     if (argc != 4) {
  696.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  697.             " stat name varName\"", (char *) NULL);
  698.         result = TCL_ERROR;
  699.         goto done;
  700.     }
  701.  
  702.     if (stat(fileName, &statBuf) == -1) {
  703.         badStat:
  704.         Tcl_AppendResult(interp, "couldn't stat \"", argv[2],
  705.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  706.         result = TCL_ERROR;
  707.         goto done;
  708.     }
  709.     result = StoreStatData(interp, argv[3], &statBuf);
  710.     goto done;
  711.     } else if ((c == 't') && (strncmp(argv[1], "type", length) == 0)
  712.         && (length >= 2)) {
  713.     if (argc != 3) {
  714.         argv[1] = "type";
  715.         goto not3Args;
  716.     }
  717.     if (lstat(fileName, &statBuf) == -1) {
  718.         goto badStat;
  719.     }
  720.     interp->result = GetFileType((int) statBuf.st_mode);
  721.     goto done;
  722.     } else {
  723.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  724.         "\": should be atime, dirname, executable, exists, ",
  725.         "extension, isdirectory, isfile, lstat, mtime, owned, ",
  726.         "readable, readlink, ",
  727.         "root, size, stat, tail, type, ",
  728.         "or writable",
  729.         (char *) NULL);
  730.     result = TCL_ERROR;
  731.     goto done;
  732.     }
  733.     if (stat(fileName, &statBuf) == -1) {
  734.     interp->result = "0";
  735.     goto done;
  736.     }
  737.     switch (statOp) {
  738.     case 0:
  739.         mode = (geteuid() == statBuf.st_uid);
  740.         break;
  741.     case 1:
  742.         mode = S_ISREG(statBuf.st_mode);
  743.         break;
  744.     case 2:
  745.         mode = S_ISDIR(statBuf.st_mode);
  746.         break;
  747.     }
  748.     if (mode) {
  749.     interp->result = "1";
  750.     } else {
  751.     interp->result = "0";
  752.     }
  753.  
  754.     done:
  755.     Tcl_DStringFree(&buffer);
  756.     return result;
  757. }
  758.  
  759. /*
  760.  *----------------------------------------------------------------------
  761.  *
  762.  * StoreStatData --
  763.  *
  764.  *    This is a utility procedure that breaks out the fields of a
  765.  *    "stat" structure and stores them in textual form into the
  766.  *    elements of an associative array.
  767.  *
  768.  * Results:
  769.  *    Returns a standard Tcl return value.  If an error occurs then
  770.  *    a message is left in interp->result.
  771.  *
  772.  * Side effects:
  773.  *    Elements of the associative array given by "varName" are modified.
  774.  *
  775.  *----------------------------------------------------------------------
  776.  */
  777.  
  778. static int
  779. StoreStatData(interp, varName, statPtr)
  780.     Tcl_Interp *interp;            /* Interpreter for error reports. */
  781.     char *varName;            /* Name of associative array variable
  782.                      * in which to store stat results. */
  783.     struct stat *statPtr;        /* Pointer to buffer containing
  784.                      * stat data to store in varName. */
  785. {
  786.     char string[30];
  787.  
  788.     sprintf(string, "%ld", statPtr->st_dev);
  789.     if (Tcl_SetVar2(interp, varName, "dev", string, TCL_LEAVE_ERR_MSG)
  790.         == NULL) {
  791.     return TCL_ERROR;
  792.     }
  793.     sprintf(string, "%ld", statPtr->st_ino);
  794.     if (Tcl_SetVar2(interp, varName, "ino", string, TCL_LEAVE_ERR_MSG)
  795.         == NULL) {
  796.     return TCL_ERROR;
  797.     }
  798.     sprintf(string, "%ld", statPtr->st_mode);
  799.     if (Tcl_SetVar2(interp, varName, "mode", string, TCL_LEAVE_ERR_MSG)
  800.         == NULL) {
  801.     return TCL_ERROR;
  802.     }
  803.     sprintf(string, "%ld", statPtr->st_nlink);
  804.     if (Tcl_SetVar2(interp, varName, "nlink", string, TCL_LEAVE_ERR_MSG)
  805.         == NULL) {
  806.     return TCL_ERROR;
  807.     }
  808.     sprintf(string, "%ld", (long) statPtr->st_uid);
  809.     if (Tcl_SetVar2(interp, varName, "uid", string, TCL_LEAVE_ERR_MSG)
  810.         == NULL) {
  811.     return TCL_ERROR;
  812.     }
  813.     sprintf(string, "%ld", (long) statPtr->st_gid);
  814.     if (Tcl_SetVar2(interp, varName, "gid", string, TCL_LEAVE_ERR_MSG)
  815.         == NULL) {
  816.     return TCL_ERROR;
  817.     }
  818.     sprintf(string, "%ld", statPtr->st_size);
  819.     if (Tcl_SetVar2(interp, varName, "size", string, TCL_LEAVE_ERR_MSG)
  820.         == NULL) {
  821.     return TCL_ERROR;
  822.     }
  823.     sprintf(string, "%ld", statPtr->st_atime);
  824.     if (Tcl_SetVar2(interp, varName, "atime", string, TCL_LEAVE_ERR_MSG)
  825.         == NULL) {
  826.     return TCL_ERROR;
  827.     }
  828.     sprintf(string, "%ld", statPtr->st_mtime);
  829.     if (Tcl_SetVar2(interp, varName, "mtime", string, TCL_LEAVE_ERR_MSG)
  830.         == NULL) {
  831.     return TCL_ERROR;
  832.     }
  833.     sprintf(string, "%ld", statPtr->st_ctime);
  834.     if (Tcl_SetVar2(interp, varName, "ctime", string, TCL_LEAVE_ERR_MSG)
  835.         == NULL) {
  836.     return TCL_ERROR;
  837.     }
  838.     if (Tcl_SetVar2(interp, varName, "type",
  839.         GetFileType((int) statPtr->st_mode), TCL_LEAVE_ERR_MSG) == NULL) {
  840.     return TCL_ERROR;
  841.     }
  842.     return TCL_OK;
  843. }
  844.  
  845. /*
  846.  *----------------------------------------------------------------------
  847.  *
  848.  * GetFileType --
  849.  *
  850.  *    Given a mode word, returns a string identifying the type of a
  851.  *    file.
  852.  *
  853.  * Results:
  854.  *    A static text string giving the file type from mode.
  855.  *
  856.  * Side effects:
  857.  *    None.
  858.  *
  859.  *----------------------------------------------------------------------
  860.  */
  861.  
  862. static char *
  863. GetFileType(mode)
  864.     int mode;
  865. {
  866.     if (S_ISREG(mode)) {
  867.     return "file";
  868.     } else if (S_ISDIR(mode)) {
  869.     return "directory";
  870.     } else if (S_ISCHR(mode)) {
  871.     return "characterSpecial";
  872.     } else if (S_ISBLK(mode)) {
  873.     return "blockSpecial";
  874.     } else if (S_ISFIFO(mode)) {
  875.     return "fifo";
  876.     } else if (S_ISLNK(mode)) {
  877.     return "link";
  878.     } else if (S_ISSOCK(mode)) {
  879.     return "socket";
  880.     }
  881.     return "unknown";
  882. }
  883.  
  884. /*
  885.  *----------------------------------------------------------------------
  886.  *
  887.  * Tcl_FlushCmd --
  888.  *
  889.  *    This procedure is invoked to process the "flush" Tcl command.
  890.  *    See the user documentation for details on what it does.
  891.  *
  892.  * Results:
  893.  *    A standard Tcl result.
  894.  *
  895.  * Side effects:
  896.  *    See the user documentation.
  897.  *
  898.  *----------------------------------------------------------------------
  899.  */
  900.  
  901.     /* ARGSUSED */
  902. int
  903. Tcl_FlushCmd(notUsed, interp, argc, argv)
  904.     ClientData notUsed;            /* Not used. */
  905.     Tcl_Interp *interp;            /* Current interpreter. */
  906.     int argc;                /* Number of arguments. */
  907.     char **argv;            /* Argument strings. */
  908. {
  909.     FILE *f;
  910.  
  911.     if (argc != 2) {
  912.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  913.         " fileId\"", (char *) NULL);
  914.     return TCL_ERROR;
  915.     }
  916.     if (Tcl_GetOpenFile(interp, argv[1], 1, 1, &f) != TCL_OK) {
  917.     return TCL_ERROR;
  918.     }
  919.     clearerr(f);
  920.     if (fflush(f) == EOF) {
  921.     Tcl_AppendResult(interp, "error flushing \"", argv[1],
  922.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  923.     return TCL_ERROR;
  924.     }
  925.     return TCL_OK;
  926. }
  927.  
  928. /*
  929.  *----------------------------------------------------------------------
  930.  *
  931.  * Tcl_GetsCmd --
  932.  *
  933.  *    This procedure is invoked to process the "gets" Tcl command.
  934.  *    See the user documentation for details on what it does.
  935.  *
  936.  * Results:
  937.  *    A standard Tcl result.
  938.  *
  939.  * Side effects:
  940.  *    See the user documentation.
  941.  *
  942.  *----------------------------------------------------------------------
  943.  */
  944.  
  945.     /* ARGSUSED */
  946. int
  947. Tcl_GetsCmd(notUsed, interp, argc, argv)
  948.     ClientData notUsed;            /* Not used. */
  949.     Tcl_Interp *interp;            /* Current interpreter. */
  950.     int argc;                /* Number of arguments. */
  951.     char **argv;            /* Argument strings. */
  952. {
  953. #   define BUF_SIZE 200
  954.     char buffer[BUF_SIZE+1];
  955.     int totalCount, done, flags;
  956.     FILE *f;
  957.  
  958.     if ((argc != 2) && (argc != 3)) {
  959.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  960.         " fileId ?varName?\"", (char *) NULL);
  961.     return TCL_ERROR;
  962.     }
  963.     if (Tcl_GetOpenFile(interp, argv[1], 0, 1, &f) != TCL_OK) {
  964.     return TCL_ERROR;
  965.     }
  966.  
  967.     /*
  968.      * We can't predict how large a line will be, so read it in
  969.      * pieces, appending to the current result or to a variable.
  970.      */
  971.  
  972.     totalCount = 0;
  973.     done = 0;
  974.     flags = 0;
  975.     clearerr(f);
  976.     while (!done) {
  977.     register int c, count;
  978.     register char *p;
  979.  
  980.     for (p = buffer, count = 0; count < BUF_SIZE-1; count++, p++) {
  981.         c = getc(f);
  982.         if (c == EOF) {
  983.         if (ferror(f)) {
  984.             /*
  985.              * If the file is in non-blocking mode, return any
  986.              * bytes that were read before a block would occur.
  987.              */
  988.  
  989.             if (((errno == EWOULDBLOCK) || (errno == EAGAIN))
  990.                 && ((count > 0 || totalCount > 0))) {
  991.             done = 1;
  992.             break;
  993.             }
  994.             Tcl_ResetResult(interp);
  995.             Tcl_AppendResult(interp, "error reading \"", argv[1],
  996.                 "\": ", Tcl_PosixError(interp), (char *) NULL);
  997.             return TCL_ERROR;
  998.         } else if (feof(f)) {
  999.             if ((totalCount == 0) && (count == 0)) {
  1000.             totalCount = -1;
  1001.             }
  1002.             done = 1;
  1003.             break;
  1004.         }
  1005.         }
  1006.         if (c == '\n') {
  1007.         done = 1;
  1008.         break;
  1009.         }
  1010.         *p = c;
  1011.     }
  1012.     *p = 0;
  1013.     if (argc == 2) {
  1014.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1015.     } else {
  1016.         if (Tcl_SetVar(interp, argv[2], buffer, flags|TCL_LEAVE_ERR_MSG)
  1017.             == NULL) {
  1018.         return TCL_ERROR;
  1019.         }
  1020.         flags = TCL_APPEND_VALUE;
  1021.     }
  1022.     totalCount += count;
  1023.     }
  1024.  
  1025.     if (argc == 3) {
  1026.     sprintf(interp->result, "%d", totalCount);
  1027.     }
  1028.     return TCL_OK;
  1029. }
  1030.  
  1031. /*
  1032.  *----------------------------------------------------------------------
  1033.  *
  1034.  * Tcl_OpenCmd --
  1035.  *
  1036.  *    This procedure is invoked to process the "open" Tcl command.
  1037.  *    See the user documentation for details on what it does.
  1038.  *
  1039.  * Results:
  1040.  *    A standard Tcl result.
  1041.  *
  1042.  * Side effects:
  1043.  *    See the user documentation.
  1044.  *
  1045.  *----------------------------------------------------------------------
  1046.  */
  1047.  
  1048.     /* ARGSUSED */
  1049. int
  1050. Tcl_OpenCmd(notUsed, interp, argc, argv)
  1051.     ClientData notUsed;            /* Not used. */
  1052.     Tcl_Interp *interp;            /* Current interpreter. */
  1053.     int argc;                /* Number of arguments. */
  1054.     char **argv;            /* Argument strings. */
  1055. {
  1056.     int pipeline, fd, mode, prot, readWrite, permissions;
  1057.     char *access;
  1058.     FILE *f, *f2;
  1059.  
  1060.     if ((argc < 2) || (argc > 4)) {
  1061.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1062.         " filename ?access? ?permissions?\"", (char *) NULL);
  1063.     return TCL_ERROR;
  1064.     }
  1065.     prot = 0666;
  1066.     if (argc == 2) {
  1067.     mode = O_RDONLY;
  1068.     access = "r";
  1069.     } else {
  1070.     access = GetOpenMode(interp, argv[2], &mode);
  1071.     if (access == NULL) {
  1072.         return TCL_ERROR;
  1073.     }
  1074.     if (argc == 4) {
  1075.         if (Tcl_GetInt(interp, argv[3], &prot) != TCL_OK) {
  1076.         return TCL_ERROR;
  1077.         }
  1078.     }
  1079.     }
  1080.  
  1081.     f = f2 = NULL;
  1082.     readWrite = mode & (O_RDWR|O_RDONLY|O_WRONLY);
  1083.     if (readWrite == O_RDONLY) {
  1084.     permissions = TCL_FILE_READABLE;
  1085.     } else if (readWrite == O_WRONLY) {
  1086.     permissions = TCL_FILE_WRITABLE;
  1087.     } else {
  1088.     permissions = TCL_FILE_READABLE|TCL_FILE_WRITABLE;
  1089.     }
  1090.  
  1091.     pipeline = 0;
  1092.     if (argv[1][0] == '|') {
  1093.     pipeline = 1;
  1094.     }
  1095.  
  1096.     /*
  1097.      * Open the file or create a process pipeline.
  1098.      */
  1099.  
  1100.     if (!pipeline) {
  1101.     char *fileName;
  1102.     Tcl_DString buffer;
  1103.  
  1104.     fileName = Tcl_TildeSubst(interp, argv[1], &buffer);
  1105.     if (fileName == NULL) {
  1106.         return TCL_ERROR;
  1107.     }
  1108.     fd = open(fileName, mode, prot);
  1109.     Tcl_DStringFree(&buffer);
  1110.     if (fd < 0) {
  1111.         Tcl_AppendResult(interp, "couldn't open \"", argv[1],
  1112.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1113.         return TCL_ERROR;
  1114.     }
  1115.     f = fdopen(fd, access);
  1116.     if (f == NULL) {
  1117.         close(fd);
  1118.         return TCL_ERROR;
  1119.     }
  1120.     Tcl_EnterFile(interp, f, permissions);
  1121.     } else {
  1122.     int *inPipePtr, *outPipePtr;
  1123.     int cmdArgc, inPipe, outPipe, numPids, *pidPtr, errorId;
  1124.     char **cmdArgv;
  1125.     OpenFile *oFilePtr;
  1126.  
  1127.     if (Tcl_SplitList(interp, argv[1]+1, &cmdArgc, &cmdArgv) != TCL_OK) {
  1128.         return TCL_ERROR;
  1129.     }
  1130.     inPipePtr = (permissions & TCL_FILE_WRITABLE) ? &inPipe : NULL;
  1131.     outPipePtr = (permissions & TCL_FILE_READABLE) ? &outPipe : NULL;
  1132.     inPipe = outPipe = errorId = -1;
  1133.     numPids = Tcl_CreatePipeline(interp, cmdArgc, cmdArgv,
  1134.         &pidPtr, inPipePtr, outPipePtr, &errorId);
  1135.     ckfree((char *) cmdArgv);
  1136.     if (numPids < 0) {
  1137.         pipelineError:
  1138.         if (f != NULL) {
  1139.         fclose(f);
  1140.         }
  1141.         if (f2 != NULL) {
  1142.         fclose(f2);
  1143.         }
  1144.         if (numPids > 0) {
  1145.         Tcl_DetachPids(numPids, pidPtr);
  1146.         ckfree((char *) pidPtr);
  1147.         }
  1148.         if (errorId != -1) {
  1149.         close(errorId);
  1150.         }
  1151.         return TCL_ERROR;
  1152.     }
  1153.     if (permissions & TCL_FILE_READABLE) {
  1154.         if (outPipe == -1) {
  1155.         if (inPipe != -1) {
  1156.             close(inPipe);
  1157.         }
  1158.         Tcl_AppendResult(interp, "can't read output from command:",
  1159.             " standard output was redirected", (char *) NULL);
  1160.         goto pipelineError;
  1161.         }
  1162.         f = fdopen(outPipe, "r");
  1163.     }
  1164.     if (permissions & TCL_FILE_WRITABLE) {
  1165.         if (inPipe == -1) {
  1166.         Tcl_AppendResult(interp, "can't write input to command:",
  1167.             " standard input was redirected", (char *) NULL);
  1168.         goto pipelineError;
  1169.         }
  1170.         if (f != NULL) {
  1171.         f2 = fdopen(inPipe, "w");
  1172.         } else {
  1173.         f = fdopen(inPipe, "w");
  1174.         }
  1175.     }
  1176.     Tcl_EnterFile(interp, f, permissions);
  1177.     oFilePtr = tclOpenFiles[fileno(f)];
  1178.     oFilePtr->f2 = f2;
  1179.     oFilePtr->numPids = numPids;
  1180.     oFilePtr->pidPtr = pidPtr;
  1181.     oFilePtr->errorId = errorId;
  1182.     }
  1183.     return TCL_OK;
  1184. }
  1185.  
  1186. /*
  1187.  *----------------------------------------------------------------------
  1188.  *
  1189.  * GetOpenMode --
  1190.  *
  1191.  *    description.
  1192.  *
  1193.  * Results:
  1194.  *    Normally, sets *modePtr to an access mode for passing to "open",
  1195.  *    and returns a string that can be used as the access mode in a
  1196.  *    subsequent call to "fdopen".  If an error occurs, then returns
  1197.  *    NULL and sets interp->result to an error message.
  1198.  *
  1199.  * Side effects:
  1200.  *    None.
  1201.  *
  1202.  * Special note:
  1203.  *    This code is based on a prototype implementation contributed
  1204.  *    by Mark Diekhans.
  1205.  *
  1206.  *----------------------------------------------------------------------
  1207.  */
  1208.  
  1209. static char *
  1210. GetOpenMode(interp, string, modePtr)
  1211.     Tcl_Interp *interp;            /* Interpreter to use for error
  1212.                      * reporting. */
  1213.     char *string;            /* Mode string, e.g. "r+" or
  1214.                      * "RDONLY CREAT". */
  1215.     int *modePtr;            /* Where to store mode corresponding
  1216.                      * to string. */
  1217. {
  1218.     int mode, modeArgc, c, i, gotRW;
  1219.     char **modeArgv, *flag;
  1220. #define RW_MODES (O_RDONLY|O_WRONLY|O_RDWR)
  1221.  
  1222.     /*
  1223.      * Check for the simpler fopen-like access modes (e.g. "r").  They
  1224.      * are distinguished from the POSIX access modes by the presence
  1225.      * of a lower-case first letter.
  1226.      */
  1227.  
  1228.     mode = 0;
  1229.     if (islower(UCHAR(string[0]))) {
  1230.     switch (string[0]) {
  1231.         case 'r':
  1232.         mode = O_RDONLY;
  1233.         break;
  1234.         case 'w':
  1235.         mode = O_WRONLY|O_CREAT|O_TRUNC;
  1236.         break;
  1237.         case 'a':
  1238.         mode = O_WRONLY|O_CREAT|O_APPEND;
  1239.         break;
  1240.         default:
  1241.         error:
  1242.         Tcl_AppendResult(interp,
  1243.             "illegal access mode \"", string, "\"", (char *) NULL);
  1244.         return NULL;
  1245.     }
  1246.     if (string[1] == '+') {
  1247.         mode &= ~(O_RDONLY|O_WRONLY);
  1248.         mode |= O_RDWR;
  1249.         if (string[2] != 0) {
  1250.         goto error;
  1251.         }
  1252.     } else if (string[1] != 0) {
  1253.         goto error;
  1254.     }
  1255.     *modePtr = mode;
  1256.     return string;
  1257.     }
  1258.  
  1259.     /*
  1260.      * The access modes are specified using a list of POSIX modes
  1261.      * such as O_CREAT.
  1262.      */
  1263.  
  1264.     if (Tcl_SplitList(interp, string, &modeArgc, &modeArgv) != TCL_OK) {
  1265.     Tcl_AddErrorInfo(interp, "\n    while processing open access modes \"");
  1266.     Tcl_AddErrorInfo(interp, string);
  1267.     Tcl_AddErrorInfo(interp, "\"");
  1268.     return NULL;
  1269.     }
  1270.     gotRW = 0;
  1271.     for (i = 0; i < modeArgc; i++) {
  1272.     flag = modeArgv[i];
  1273.     c = flag[0];
  1274.     if ((c == 'R') && (strcmp(flag, "RDONLY") == 0)) {
  1275.         mode = (mode & ~RW_MODES) | O_RDONLY;
  1276.         gotRW = 1;
  1277.     } else if ((c == 'W') && (strcmp(flag, "WRONLY") == 0)) {
  1278.         mode = (mode & ~RW_MODES) | O_WRONLY;
  1279.         gotRW = 1;
  1280.     } else if ((c == 'R') && (strcmp(flag, "RDWR") == 0)) {
  1281.         mode = (mode & ~RW_MODES) | O_RDWR;
  1282.         gotRW = 1;
  1283.     } else if ((c == 'A') && (strcmp(flag, "APPEND") == 0)) {
  1284.         mode |= O_APPEND;
  1285.     } else if ((c == 'C') && (strcmp(flag, "CREAT") == 0)) {
  1286.         mode |= O_CREAT;
  1287.     } else if ((c == 'E') && (strcmp(flag, "EXCL") == 0)) {
  1288.         mode |= O_EXCL;
  1289.     } else if ((c == 'N') && (strcmp(flag, "NOCTTY") == 0)) {
  1290. #ifdef O_NOCTTY
  1291.         mode |= O_NOCTTY;
  1292. #else
  1293.         Tcl_AppendResult(interp, "access mode \"", flag,
  1294.             "\" not supported by this system", (char *) NULL);
  1295.         ckfree((char *) modeArgv);
  1296.         return NULL;
  1297. #endif
  1298.     } else if ((c == 'N') && (strcmp(flag, "NONBLOCK") == 0)) {
  1299. #ifdef O_NONBLOCK
  1300.         mode |= O_NONBLOCK;
  1301. #else
  1302.         mode |= O_NDELAY;
  1303. #endif
  1304.     } else if ((c == 'T') && (strcmp(flag, "TRUNC") == 0)) {
  1305.         mode |= O_TRUNC;
  1306.     } else {
  1307.         Tcl_AppendResult(interp, "invalid access mode \"", flag,
  1308.             "\": must be RDONLY, WRONLY, RDWR, APPEND, CREAT",
  1309.             " EXCL, NOCTTY, NONBLOCK, or TRUNC", (char *) NULL);
  1310.         ckfree((char *) modeArgv);
  1311.         return NULL;
  1312.     }
  1313.     }
  1314.     ckfree((char *) modeArgv);
  1315.     if (!gotRW) {
  1316.     Tcl_AppendResult(interp, "access mode must include either",
  1317.         " RDONLY, WRONLY, or RDWR", (char *) NULL);
  1318.     return NULL;
  1319.     }
  1320.     *modePtr = mode;
  1321.  
  1322.     /*
  1323.      * The calculation of fdopen access mode below isn't really correct,
  1324.      * but it doesn't have to be.  All it has to do is to disinguish
  1325.      * read and write permissions, plus indicate append mode.
  1326.      */
  1327.  
  1328.     i = mode & RW_MODES;
  1329.     if (i == O_RDONLY) {
  1330.     return "r";
  1331.     }
  1332.     if (mode & O_APPEND) {
  1333.     if (i == O_WRONLY) {
  1334.         return "a";
  1335.     } else {
  1336.         return "a+";
  1337.     }
  1338.     }
  1339.     if (i == O_WRONLY) {
  1340.     return "w";
  1341.     }
  1342.     return "r+";
  1343. }
  1344.  
  1345. /*
  1346.  *----------------------------------------------------------------------
  1347.  *
  1348.  * Tcl_PidCmd --
  1349.  *
  1350.  *    This procedure is invoked to process the "pid" Tcl command.
  1351.  *    See the user documentation for details on what it does.
  1352.  *
  1353.  * Results:
  1354.  *    A standard Tcl result.
  1355.  *
  1356.  * Side effects:
  1357.  *    See the user documentation.
  1358.  *
  1359.  *----------------------------------------------------------------------
  1360.  */
  1361.  
  1362.     /* ARGSUSED */
  1363. int
  1364. Tcl_PidCmd(dummy, interp, argc, argv)
  1365.     ClientData dummy;            /* Not used. */
  1366.     Tcl_Interp *interp;            /* Current interpreter. */
  1367.     int argc;                /* Number of arguments. */
  1368.     char **argv;            /* Argument strings. */
  1369. {
  1370.     FILE *f;
  1371.     OpenFile *oFilePtr;
  1372.     int i;
  1373.     char string[50];
  1374.  
  1375.     if (argc > 2) {
  1376.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1377.         argv[0], " ?fileId?\"", (char *) NULL);
  1378.     return TCL_ERROR;
  1379.     }
  1380.     if (argc == 1) {
  1381.     sprintf(interp->result, "%ld", (long) getpid());
  1382.     } else {
  1383.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1384.         return TCL_ERROR;
  1385.     }
  1386.     oFilePtr = tclOpenFiles[fileno(f)];
  1387.     for (i = 0; i < oFilePtr->numPids; i++) {
  1388.         sprintf(string, "%d", oFilePtr->pidPtr[i]);
  1389.         Tcl_AppendElement(interp, string);
  1390.     }
  1391.     }
  1392.     return TCL_OK;
  1393. }
  1394.  
  1395. /*
  1396.  *----------------------------------------------------------------------
  1397.  *
  1398.  * Tcl_PutsCmd --
  1399.  *
  1400.  *    This procedure is invoked to process the "puts" Tcl command.
  1401.  *    See the user documentation for details on what it does.
  1402.  *
  1403.  * Results:
  1404.  *    A standard Tcl result.
  1405.  *
  1406.  * Side effects:
  1407.  *    See the user documentation.
  1408.  *
  1409.  *----------------------------------------------------------------------
  1410.  */
  1411.  
  1412.     /* ARGSUSED */
  1413. int
  1414. Tcl_PutsCmd(dummy, interp, argc, argv)
  1415.     ClientData dummy;            /* Not used. */
  1416.     Tcl_Interp *interp;            /* Current interpreter. */
  1417.     int argc;                /* Number of arguments. */
  1418.     char **argv;            /* Argument strings. */
  1419. {
  1420.     FILE *f;
  1421.     int i, newline;
  1422.     char *fileId;
  1423.  
  1424.     i = 1;
  1425.     newline = 1;
  1426.     if ((argc >= 2) && (strcmp(argv[1], "-nonewline") == 0)) {
  1427.     newline = 0;
  1428.     i++;
  1429.     }
  1430.     if ((i < (argc-3)) || (i >= argc)) {
  1431.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1432.         " ?-nonewline? ?fileId? string\"", (char *) NULL);
  1433.     return TCL_ERROR;
  1434.     }
  1435.  
  1436.     /*
  1437.      * The code below provides backwards compatibility with an old
  1438.      * form of the command that is no longer recommended or documented.
  1439.      */
  1440.  
  1441.     if (i == (argc-3)) {
  1442.     if (strncmp(argv[i+2], "nonewline", strlen(argv[i+2])) != 0) {
  1443.         Tcl_AppendResult(interp, "bad argument \"", argv[i+2],
  1444.             "\": should be \"nonewline\"", (char *) NULL);
  1445.         return TCL_ERROR;
  1446.     }
  1447.     newline = 0;
  1448.     }
  1449.     if (i == (argc-1)) {
  1450.     fileId = "stdout";
  1451.     } else {
  1452.     fileId = argv[i];
  1453.     i++;
  1454.     }
  1455.  
  1456.     if (Tcl_GetOpenFile(interp, fileId, 1, 1, &f) != TCL_OK) {
  1457.     return TCL_ERROR;
  1458.     }
  1459.  
  1460.     clearerr(f);
  1461.     fputs(argv[i], f);
  1462.     if (newline) {
  1463.     fputc('\n', f);
  1464.     }
  1465.     if (ferror(f)) {
  1466.     Tcl_AppendResult(interp, "error writing \"", fileId,
  1467.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  1468.     return TCL_ERROR;
  1469.     }
  1470.     return TCL_OK;
  1471. }
  1472.  
  1473. /*
  1474.  *----------------------------------------------------------------------
  1475.  *
  1476.  * Tcl_PwdCmd --
  1477.  *
  1478.  *    This procedure is invoked to process the "pwd" Tcl command.
  1479.  *    See the user documentation for details on what it does.
  1480.  *
  1481.  * Results:
  1482.  *    A standard Tcl result.
  1483.  *
  1484.  * Side effects:
  1485.  *    See the user documentation.
  1486.  *
  1487.  *----------------------------------------------------------------------
  1488.  */
  1489.  
  1490.     /* ARGSUSED */
  1491. int
  1492. Tcl_PwdCmd(dummy, interp, argc, argv)
  1493.     ClientData dummy;            /* Not used. */
  1494.     Tcl_Interp *interp;            /* Current interpreter. */
  1495.     int argc;                /* Number of arguments. */
  1496.     char **argv;            /* Argument strings. */
  1497. {
  1498.     char buffer[MAXPATHLEN+1];
  1499.  
  1500.     if (argc != 1) {
  1501.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1502.         argv[0], "\"", (char *) NULL);
  1503.     return TCL_ERROR;
  1504.     }
  1505.     if (currentDir == NULL) {
  1506.     if (getcwd(buffer, MAXPATHLEN+1) == NULL) {
  1507.         if (errno == ERANGE) {
  1508.         interp->result = "working directory name is too long";
  1509.         } else {
  1510.         Tcl_AppendResult(interp,
  1511.             "error getting working directory name: ",
  1512.             Tcl_PosixError(interp), (char *) NULL);
  1513.         }
  1514.         return TCL_ERROR;
  1515.     }
  1516.     currentDir = (char *) ckalloc((unsigned) (strlen(buffer) + 1));
  1517.     strcpy(currentDir, buffer);
  1518.     }
  1519.     interp->result = currentDir;
  1520.     return TCL_OK;
  1521. }
  1522.  
  1523. /*
  1524.  *----------------------------------------------------------------------
  1525.  *
  1526.  * Tcl_ReadCmd --
  1527.  *
  1528.  *    This procedure is invoked to process the "read" Tcl command.
  1529.  *    See the user documentation for details on what it does.
  1530.  *
  1531.  * Results:
  1532.  *    A standard Tcl result.
  1533.  *
  1534.  * Side effects:
  1535.  *    See the user documentation.
  1536.  *
  1537.  *----------------------------------------------------------------------
  1538.  */
  1539.  
  1540.     /* ARGSUSED */
  1541. int
  1542. Tcl_ReadCmd(dummy, interp, argc, argv)
  1543.     ClientData dummy;            /* Not used. */
  1544.     Tcl_Interp *interp;            /* Current interpreter. */
  1545.     int argc;                /* Number of arguments. */
  1546.     char **argv;            /* Argument strings. */
  1547. {
  1548.     int bytesLeft, bytesRead, askedFor, got;
  1549. #define READ_BUF_SIZE 4096
  1550.     char buffer[READ_BUF_SIZE+1];
  1551.     int newline, i;
  1552.     FILE *f;
  1553.  
  1554.     if ((argc != 2) && (argc != 3)) {
  1555.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1556.         " fileId ?numBytes?\" or \"", argv[0],
  1557.         " ?-nonewline? fileId\"", (char *) NULL);
  1558.     return TCL_ERROR;
  1559.     }
  1560.     i = 1;
  1561.     newline = 1;
  1562.     if ((argc == 3) && (strcmp(argv[1], "-nonewline") == 0)) {
  1563.     newline = 0;
  1564.     i++;
  1565.     }
  1566.     if (Tcl_GetOpenFile(interp, argv[i], 0, 1, &f) != TCL_OK) {
  1567.     return TCL_ERROR;
  1568.     }
  1569.  
  1570.     /*
  1571.      * Compute how many bytes to read, and see whether the final
  1572.      * newline should be dropped.
  1573.      */
  1574.  
  1575.     if ((argc >= (i + 2)) && isdigit(UCHAR(argv[i+1][0]))) {
  1576.     if (Tcl_GetInt(interp, argv[i+1], &bytesLeft) != TCL_OK) {
  1577.         return TCL_ERROR;
  1578.     }
  1579.     } else {
  1580.     bytesLeft = INT_MAX;
  1581.  
  1582.     /*
  1583.      * The code below provides backward compatibility for an
  1584.      * archaic earlier version of this command.
  1585.      */
  1586.  
  1587.     if (argc >= (i + 2)) {
  1588.         if (strncmp(argv[i+1], "nonewline", strlen(argv[i+1])) == 0) {
  1589.         newline = 0;
  1590.         } else {
  1591.         Tcl_AppendResult(interp, "bad argument \"", argv[i+1],
  1592.             "\": should be \"nonewline\"", (char *) NULL);
  1593.         return TCL_ERROR;
  1594.         }
  1595.     }
  1596.     }
  1597.  
  1598.     /*
  1599.      * Read the file in one or more chunks.
  1600.      */
  1601.  
  1602.     bytesRead = 0;
  1603.     clearerr(f);
  1604.     while (bytesLeft > 0) {
  1605.     askedFor = READ_BUF_SIZE;
  1606.     if (bytesLeft < READ_BUF_SIZE) {
  1607.         askedFor = bytesLeft;
  1608.     }
  1609.     got = fread(buffer, 1, (size_t) askedFor, f);
  1610.     if (ferror(f)) {
  1611.         /*
  1612.          * If the file is in non-blocking mode, break out of the
  1613.          * loop and return any bytes that were read.
  1614.          */
  1615.  
  1616.         if (((errno == EWOULDBLOCK) || (errno == EAGAIN))
  1617.             && ((got > 0) || (bytesRead > 0))) {
  1618.         clearerr(f);
  1619.         bytesLeft = got;
  1620.         } else {
  1621.         Tcl_ResetResult(interp);
  1622.         Tcl_AppendResult(interp, "error reading \"", argv[i],
  1623.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1624.         return TCL_ERROR;
  1625.         }
  1626.     }
  1627.     if (got != 0) {
  1628.         buffer[got] = 0;
  1629.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1630.         bytesLeft -= got;
  1631.         bytesRead += got;
  1632.     }
  1633.     if (got < askedFor) {
  1634.         break;
  1635.     }
  1636.     }
  1637.     if ((newline == 0) && (bytesRead > 0)
  1638.         && (interp->result[bytesRead-1] == '\n')) {
  1639.     interp->result[bytesRead-1] = 0;
  1640.     }
  1641.     return TCL_OK;
  1642. }
  1643.  
  1644. /*
  1645.  *----------------------------------------------------------------------
  1646.  *
  1647.  * Tcl_SeekCmd --
  1648.  *
  1649.  *    This procedure is invoked to process the "seek" Tcl command.
  1650.  *    See the user documentation for details on what it does.
  1651.  *
  1652.  * Results:
  1653.  *    A standard Tcl result.
  1654.  *
  1655.  * Side effects:
  1656.  *    See the user documentation.
  1657.  *
  1658.  *----------------------------------------------------------------------
  1659.  */
  1660.  
  1661.     /* ARGSUSED */
  1662. int
  1663. Tcl_SeekCmd(notUsed, interp, argc, argv)
  1664.     ClientData notUsed;            /* Not used. */
  1665.     Tcl_Interp *interp;            /* Current interpreter. */
  1666.     int argc;                /* Number of arguments. */
  1667.     char **argv;            /* Argument strings. */
  1668. {
  1669.     FILE *f;
  1670.     int offset, mode;
  1671.  
  1672.     if ((argc != 3) && (argc != 4)) {
  1673.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1674.         " fileId offset ?origin?\"", (char *) NULL);
  1675.     return TCL_ERROR;
  1676.     }
  1677.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1678.     return TCL_ERROR;
  1679.     }
  1680.     if (Tcl_GetInt(interp, argv[2], &offset) != TCL_OK) {
  1681.     return TCL_ERROR;
  1682.     }
  1683.     mode = SEEK_SET;
  1684.     if (argc == 4) {
  1685.     size_t length;
  1686.     int c;
  1687.  
  1688.     length = strlen(argv[3]);
  1689.     c = argv[3][0];
  1690.     if ((c == 's') && (strncmp(argv[3], "start", length) == 0)) {
  1691.         mode = SEEK_SET;
  1692.     } else if ((c == 'c') && (strncmp(argv[3], "current", length) == 0)) {
  1693.         mode = SEEK_CUR;
  1694.     } else if ((c == 'e') && (strncmp(argv[3], "end", length) == 0)) {
  1695.         mode = SEEK_END;
  1696.     } else {
  1697.         Tcl_AppendResult(interp, "bad origin \"", argv[3],
  1698.             "\": should be start, current, or end", (char *) NULL);
  1699.         return TCL_ERROR;
  1700.     }
  1701.     }
  1702.     clearerr(f);
  1703.     if (fseek(f, (long) offset, mode) == -1) {
  1704.     Tcl_AppendResult(interp, "error during seek: ",
  1705.         Tcl_PosixError(interp), (char *) NULL);
  1706.     return TCL_ERROR;
  1707.     }
  1708.  
  1709.     return TCL_OK;
  1710. }
  1711.  
  1712. /*
  1713.  *----------------------------------------------------------------------
  1714.  *
  1715.  * Tcl_SourceCmd --
  1716.  *
  1717.  *    This procedure is invoked to process the "source" Tcl command.
  1718.  *    See the user documentation for details on what it does.
  1719.  *
  1720.  * Results:
  1721.  *    A standard Tcl result.
  1722.  *
  1723.  * Side effects:
  1724.  *    See the user documentation.
  1725.  *
  1726.  *----------------------------------------------------------------------
  1727.  */
  1728.  
  1729.     /* ARGSUSED */
  1730. int
  1731. Tcl_SourceCmd(dummy, interp, argc, argv)
  1732.     ClientData dummy;            /* Not used. */
  1733.     Tcl_Interp *interp;            /* Current interpreter. */
  1734.     int argc;                /* Number of arguments. */
  1735.     char **argv;            /* Argument strings. */
  1736. {
  1737.     if (argc != 2) {
  1738.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1739.         " fileName\"", (char *) NULL);
  1740.     return TCL_ERROR;
  1741.     }
  1742.     return Tcl_EvalFile(interp, argv[1]);
  1743. }
  1744.  
  1745. /*
  1746.  *----------------------------------------------------------------------
  1747.  *
  1748.  * Tcl_TellCmd --
  1749.  *
  1750.  *    This procedure is invoked to process the "tell" Tcl command.
  1751.  *    See the user documentation for details on what it does.
  1752.  *
  1753.  * Results:
  1754.  *    A standard Tcl result.
  1755.  *
  1756.  * Side effects:
  1757.  *    See the user documentation.
  1758.  *
  1759.  *----------------------------------------------------------------------
  1760.  */
  1761.  
  1762.     /* ARGSUSED */
  1763. int
  1764. Tcl_TellCmd(notUsed, interp, argc, argv)
  1765.     ClientData notUsed;            /* Not used. */
  1766.     Tcl_Interp *interp;            /* Current interpreter. */
  1767.     int argc;                /* Number of arguments. */
  1768.     char **argv;            /* Argument strings. */
  1769. {
  1770.     FILE *f;
  1771.  
  1772.     if (argc != 2) {
  1773.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1774.         " fileId\"", (char *) NULL);
  1775.     return TCL_ERROR;
  1776.     }
  1777.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1778.     return TCL_ERROR;
  1779.     }
  1780.     sprintf(interp->result, "%ld", (long int) ftell(f));
  1781.     return TCL_OK;
  1782. }
  1783.  
  1784. /*
  1785.  *----------------------------------------------------------------------
  1786.  *
  1787.  * Tcl_TimeCmd --
  1788.  *
  1789.  *    This procedure is invoked to process the "time" Tcl command.
  1790.  *    See the user documentation for details on what it does.
  1791.  *
  1792.  * Results:
  1793.  *    A standard Tcl result.
  1794.  *
  1795.  * Side effects:
  1796.  *    See the user documentation.
  1797.  *
  1798.  *----------------------------------------------------------------------
  1799.  */
  1800.  
  1801.     /* ARGSUSED */
  1802. int
  1803. Tcl_TimeCmd(dummy, interp, argc, argv)
  1804.     ClientData dummy;            /* Not used. */
  1805.     Tcl_Interp *interp;            /* Current interpreter. */
  1806.     int argc;                /* Number of arguments. */
  1807.     char **argv;            /* Argument strings. */
  1808. {
  1809.     int count, i, result;
  1810.     double timePer;
  1811. #if NO_GETTOD
  1812.     struct tms dummy2;
  1813.     long start, stop;
  1814. #else
  1815.     struct timeval start, stop;
  1816.     struct timezone tz;
  1817.     int micros;
  1818. #endif
  1819.  
  1820.     if (argc == 2) {
  1821.     count = 1;
  1822.     } else if (argc == 3) {
  1823.     if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
  1824.         return TCL_ERROR;
  1825.     }
  1826.     } else {
  1827.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1828.         " command ?count?\"", (char *) NULL);
  1829.     return TCL_ERROR;
  1830.     }
  1831. #if NO_GETTOD
  1832.     start = times(&dummy2);
  1833. #else
  1834.     gettimeofday(&start, &tz);
  1835. #endif
  1836.     for (i = count ; i > 0; i--) {
  1837.     result = Tcl_Eval(interp, argv[1]);
  1838.     if (result != TCL_OK) {
  1839.         if (result == TCL_ERROR) {
  1840.         char msg[60];
  1841.         sprintf(msg, "\n    (\"time\" body line %d)",
  1842.             interp->errorLine);
  1843.         Tcl_AddErrorInfo(interp, msg);
  1844.         }
  1845.         return result;
  1846.     }
  1847.     }
  1848. #if NO_GETTOD
  1849.     stop = times(&dummy2);
  1850.     timePer = (((double) (stop - start))*1000000.0)/CLK_TCK;
  1851. #else
  1852.     gettimeofday(&stop, &tz);
  1853.     micros = (stop.tv_sec - start.tv_sec)*1000000
  1854.         + (stop.tv_usec - start.tv_usec);
  1855.     timePer = micros;
  1856. #endif
  1857.     Tcl_ResetResult(interp);
  1858.     sprintf(interp->result, "%.0f microseconds per iteration", timePer/count);
  1859.     return TCL_OK;
  1860. }
  1861.  
  1862. /*
  1863.  *----------------------------------------------------------------------
  1864.  *
  1865.  * CleanupChildren --
  1866.  *
  1867.  *    This is a utility procedure used to wait for child processes
  1868.  *    to exit, record information about abnormal exits, and then
  1869.  *    collect any stderr output generated by them.
  1870.  *
  1871.  * Results:
  1872.  *    The return value is a standard Tcl result.  If anything at
  1873.  *    weird happened with the child processes, TCL_ERROR is returned
  1874.  *    and a message is left in interp->result.
  1875.  *
  1876.  * Side effects:
  1877.  *    If the last character of interp->result is a newline, then it
  1878.  *    is removed unless keepNewline is non-zero.  File errorId gets
  1879.  *    closed, and pidPtr is freed back to the storage allocator.
  1880.  *
  1881.  *----------------------------------------------------------------------
  1882.  */
  1883.  
  1884. static int
  1885. CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  1886.     Tcl_Interp *interp;        /* Used for error messages. */
  1887.     int numPids;        /* Number of entries in pidPtr array. */
  1888.     int *pidPtr;        /* Array of process ids of children. */
  1889.     int errorId;        /* File descriptor index for file containing
  1890.                  * stderr output from pipeline.  -1 means
  1891.                  * there isn't any stderr output. */
  1892.     int keepNewline;        /* Non-zero means don't discard trailing
  1893.                  * newline. */
  1894. {
  1895.     int result = TCL_OK;
  1896.     int i, pid, length, abnormalExit;
  1897.     WAIT_STATUS_TYPE waitStatus;
  1898.  
  1899.     abnormalExit = 0;
  1900.     for (i = 0; i < numPids; i++) {
  1901.     pid = waitpid(pidPtr[i], (int *) &waitStatus, 0);
  1902.     if (pid == -1) {
  1903.         Tcl_AppendResult(interp, "error waiting for process to exit: ",
  1904.             Tcl_PosixError(interp), (char *) NULL);
  1905.         continue;
  1906.     }
  1907.  
  1908.     /*
  1909.      * Create error messages for unusual process exits.  An
  1910.      * extra newline gets appended to each error message, but
  1911.      * it gets removed below (in the same fashion that an
  1912.      * extra newline in the command's output is removed).
  1913.      */
  1914.  
  1915.     if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) {
  1916.         char msg1[20], msg2[20];
  1917.  
  1918.         result = TCL_ERROR;
  1919.         sprintf(msg1, "%d", pid);
  1920.         if (WIFEXITED(waitStatus)) {
  1921.         sprintf(msg2, "%d", WEXITSTATUS(waitStatus));
  1922.         Tcl_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2,
  1923.             (char *) NULL);
  1924.         abnormalExit = 1;
  1925.         } else if (WIFSIGNALED(waitStatus)) {
  1926.         char *p;
  1927.     
  1928.         p = Tcl_SignalMsg((int) (WTERMSIG(waitStatus)));
  1929.         Tcl_SetErrorCode(interp, "CHILDKILLED", msg1,
  1930.             Tcl_SignalId((int) (WTERMSIG(waitStatus))), p,
  1931.             (char *) NULL);
  1932.         Tcl_AppendResult(interp, "child killed: ", p, "\n",
  1933.             (char *) NULL);
  1934.         } else if (WIFSTOPPED(waitStatus)) {
  1935.         char *p;
  1936.  
  1937.         p = Tcl_SignalMsg((int) (WSTOPSIG(waitStatus)));
  1938.         Tcl_SetErrorCode(interp, "CHILDSUSP", msg1,
  1939.             Tcl_SignalId((int) (WSTOPSIG(waitStatus))), p, (char *) NULL);
  1940.         Tcl_AppendResult(interp, "child suspended: ", p, "\n",
  1941.             (char *) NULL);
  1942.         } else {
  1943.         Tcl_AppendResult(interp,
  1944.             "child wait status didn't make sense\n",
  1945.             (char *) NULL);
  1946.         }
  1947.     }
  1948.     }
  1949.     ckfree((char *) pidPtr);
  1950.  
  1951.     /*
  1952.      * Read the standard error file.  If there's anything there,
  1953.      * then return an error and add the file's contents to the result
  1954.      * string.
  1955.      */
  1956.  
  1957.     if (errorId >= 0) {
  1958.     while (1) {
  1959. #        define BUFFER_SIZE 1000
  1960.         char buffer[BUFFER_SIZE+1];
  1961.         int count;
  1962.     
  1963.         count = read(errorId, buffer, (size_t) BUFFER_SIZE);
  1964.     
  1965.         if (count == 0) {
  1966.         break;
  1967.         }
  1968.         result = TCL_ERROR;
  1969.         if (count < 0) {
  1970.         Tcl_AppendResult(interp,
  1971.             "error reading stderr output file: ",
  1972.             Tcl_PosixError(interp), (char *) NULL);
  1973.         break;
  1974.         }
  1975.         buffer[count] = 0;
  1976.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1977.     }
  1978.     close(errorId);
  1979.     }
  1980.  
  1981.     /*
  1982.      * If a child exited abnormally but didn't output any error information
  1983.      * at all, generate an error message here.
  1984.      */
  1985.  
  1986.     if (abnormalExit && (*interp->result == 0)) {
  1987.     Tcl_AppendResult(interp, "child process exited abnormally",
  1988.         (char *) NULL);
  1989.     }
  1990.  
  1991.     /*
  1992.      * If the last character of interp->result is a newline, then remove
  1993.      * the newline character (the newline would just confuse things).
  1994.      * Special hack: must replace the old terminating null character
  1995.      * as a signal to Tcl_AppendResult et al. that we've mucked with
  1996.      * the string.
  1997.      */
  1998.  
  1999.     length = strlen(interp->result);
  2000.     if (!keepNewline && (length > 0) && (interp->result[length-1] == '\n')) {
  2001.     interp->result[length-1] = '\0';
  2002.     interp->result[length] = 'x';
  2003.     }
  2004.  
  2005.     return result;
  2006. }
  2007.